// pages/discount/discount.js
import {
    requestUtil,
    getbaseUrl
} from "../../utils/request"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        discountData: [],
        disActiveDate: [],
        undisActiveDate: [],
        hei:0,
        totalPrice:0
    },

    // 过滤数据
    filterData(data) {

        if(data.length==0){
            this.setData({
                disActiveDate: [],
                undisActiveDate: []
            })
            return
        }

        let newData = data.map(item => {
            let startyear = item.startDate.split(" ")[0].split("-")
            let endtyear = item.dealDate.split(" ")[0].split("-")
            item.startDate =startyear[0] + "." +startyear[1] + "." +startyear[2]
            item.dealDate =endtyear[0] + "." +endtyear[1] + "." +endtyear[2]
        //    console.log(item)
           return item
        })

       
        let arr1 = newData.filter(item => {

            return item.disActive == 2
        })


        this.setData({
            disActiveDate: arr1,
        })
    },

    // 获取用户的优惠券

    getuserDiscount() {
        let openId = this.data.userInfo.openid
        // console.log(openId)
        let res = requestUtil({
            url: "/shop/getUserAddDiscount",
            method: "get",
            data: {
                openId
            }
        })
        res.then(({
            data
        }) => {
            // console.log(data)
            if (data.status == 200) {
                // 对比数据渲染
                this.filterData(data.data)
                this.setData({
                    discountData: data.data
                })
            } else {
                this.filterData([])
                this.setData({
                    discountData: []
                })
            }
        })
    },

    // 获取用户的信息
    getuserInfo() {
        wx.getStorage({
            key: "userInfo",
            success: (res) => {
                let userInfo = JSON.parse(res.data)
                // console.log(userInfo)
                this.setData({
                    userInfo
                })

            },
            fail: (res) => {
                this.setData({
                    userInfo: {}
                })
            }
        })
    },

    // 使用优惠券
    tostore(){
        wx.switchTab({
          url: '/pages/address/address',
        })
    },

    // 更改istrue变量
    changeIsTrue(e){
        let totalPrice=  this.data.totalPrice
        let {istrue,userid,rmb} = e.currentTarget.dataset
        if(totalPrice<rmb)return
        // console.log(totalPrice,rmb,e.currentTarget.dataset)
        // console.log(totalPrice<rmb)
        istrue = istrue==1?0:1
        let openId = this.data.userInfo.openid

        let res = requestUtil({
            url:"/shop/changedisIstrue",
            method:"get",
            data:{
                istrue,
                userid,
                openId
            }
        })

        res.then(({data})=>{
          
            if(data.status==200){
                this.getuserDiscount()
                wx.hideLoading()
            }else{
                wx.hideLoading()
            }
        })
 
    },

    // 返回
    back(){
        wx.navigateBack({
          delta: 1,
        })
    },

    // 获取屏幕高度
    getHei(){
        wx.getSystemInfo({
          success: (result) => {

              this.setData({
                  hei:result.windowHeight
              })
          },
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(options){
            this.setData({
                totalPrice:parseFloat(options.totalPrice)
            })
        }
       
        this.getuserInfo()
        setTimeout(() => {
            this.getuserDiscount()
        }, 100)
        this.getHei()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})